package com.shigen.demo.pattern.singleton;

/**
 * 懒汉式单例
 * 优点：节省资源、线程安全
 * 缺点：影响了执行性能
 *
 * @author shigenfu
 * @date 2022/8/14 11:26 上午
 */
public class LazySingleton {

  private static LazySingleton instance;

  private LazySingleton() {

  }

  public static synchronized LazySingleton getInstance() {
    if (instance == null) {
      instance = new LazySingleton();
    }
    return instance;
  }

  public static void main(String[] args) {
    Thread aaa = new Thread(() -> {
      System.out.println(LazySingleton.getInstance());
    }, "AAA");

    Thread bbb = new Thread(() -> {
      System.out.println(LazySingleton.getInstance());
    }, "BBB");

    Thread ccc = new Thread(() -> {
      System.out.println(LazySingleton.getInstance());
    }, "CCC");


    aaa.start();
    bbb.start();
    ccc.start();
  }

}
